﻿using Lung_Sounds_V2;
using LungSoundWizard.Classes;
using LungSoundWizard.Windows;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace LungSoundWizard.ViewModel
{
    public class EditRefSoundsViewModel : WizardViewModelBase
    {
        public override string Title
        {
            get { return "Edit reference sounds"; }
        }

        RelayCommand _renameSnippetCommand;
        public ICommand RenameSnippetCommand
        {
            get
            {
                if (_renameSnippetCommand == null)
                {
                    _renameSnippetCommand = new RelayCommand(p => RenameSnippet((ReferenceSnippet)p),
                        p => p != null);
                }
                return _renameSnippetCommand;
            }
        }

        RelayCommand _addSnippetCommand;
        public ICommand AddSnippetCommand
        {
            get
            {
                if (_addSnippetCommand == null)
                {
                    _addSnippetCommand = new RelayCommand(p => AddSnippet());
                }
                return _addSnippetCommand;
            }
        }

        RelayCommand _deleteSnippetsCommand;
        public ICommand DeleteSnippetsCommand
        {
            get
            {
                if (_deleteSnippetsCommand == null)
                {
                    _deleteSnippetsCommand = new RelayCommand(p => DeleteSnippets(((IList)p).Cast<ReferenceSnippet>().ToList()), 
                        p => p != null && ((IList)p).Count > 0);
                }
                return _deleteSnippetsCommand;
            }
        }

        RelayCommand _viewAvgCommand;
        public ICommand ViewAvgCommand
        {
            get
            {
                if (_viewAvgCommand == null)
                {
                    _viewAvgCommand = new RelayCommand(p => ViewAvg());
                }
                return _viewAvgCommand;
            }
        }

        void RenameSnippet(ReferenceSnippet snippet)
        {
            RenameWindow renameWin = new RenameWindow();

            renameWin.Name = snippet.Name;

            if (renameWin.ShowDialog() == true)
            {
                snippet.Name = renameWin.Name;
            }
        }

        void AddSnippet()
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Filter = "Wave file (*.wav)|*.wav";

            if (openDialog.ShowDialog() == true)
            {
                SelectSnippetViewModel ssVM = new SelectSnippetViewModel(openDialog.FileName);
                SelectSnippetWindow ssWindow = new SelectSnippetWindow(ssVM);
                ssWindow.ShowDialog();

                foreach (ReferenceSnippet snippet in ssVM.Snippets)
                {
                    ReferenceSnippetReader.Snippets.AddSnippet(snippet);
                }
            }

            ReferenceSnippetReader.Save();
        }

        void DeleteSnippets(List<ReferenceSnippet> snippets)
        {
            foreach (ReferenceSnippet snippet in snippets)
            {
                ReferenceSnippetReader.Snippets.RemoveSnippet(snippet);
            }
        }

        void ViewAvg()
        {
            Dictionary<string, double[]> data = new Dictionary<string, double[]>(3);

            data.Add("Average", ReferenceSnippetReader.Snippets.Average);
            data.Add("StDev+", ReferenceSnippetReader.Snippets.StDev);
            data.Add("StDev-", ReferenceSnippetReader.Snippets.StDevNeg);

            new GraphWindow(data, "Average 'normal' lung sound, SNR", "Frequency (Hz)", "SNR (dB)", 10.76660156, 1).Show();
        }
    }
}
